package top.fkxuexi.blogapi.core.utils;

import lombok.Getter;
import lombok.Setter;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

/**
 * 加密工具类
 */
public class EncryptUtils {


    public enum EncryptType{

        MDG("MD5"),
        SHA_1("SHA-1"),
        SHA_256("SHA-256");

        @Setter@Getter private String type;
        EncryptType(String type){
            this.type = type;
        }
    }

    public static String encrypt(String encryptStr){
        return encrypt(encryptStr,1,"",EncryptType.MDG);
    }

    public static String encrypt(String encryptStr,int time){
        return encrypt(encryptStr,time,"",EncryptType.MDG);
    }

    public static String encrypt(String encryptStr,int time, String salt){
        return encrypt(encryptStr,time,salt,EncryptType.MDG);
    }

    public static String encrypt(String encryptStr,int time,EncryptType type){
        return encrypt(encryptStr,time,"",EncryptType.MDG);
    }

    /**
     * @param encryptStr 加密字符串
     * @param time 加密次数
     * @param salt 盐值
     * @param type 加密算法
     * @return 返回的是经过加盐以及多次加密，然后base64编码过后的加密密码
     */
    public static String encrypt(String encryptStr,int time,String salt,EncryptType type){
        SimpleHash simpleHash = new SimpleHash(type.getType(), encryptStr, salt, time);
        return simpleHash.toBase64();
    }

    /**
     * @param encryptStr
     * @param time
     * @param salt
     * @param type
     * @return
     */
    public static String encrypt(String encryptStr, int time, ByteSource salt, EncryptType type){
        SimpleHash simpleHash = new SimpleHash(type.getType(), encryptStr, salt, time);
        return simpleHash.toBase64();
    }

}
